import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',  
    redirect: '/login',  
  },
  {
    path: '/login',
    name: 'login',
    component: ()=> import(/* webpackChunkName: "login" */'../pages/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    redirect:'/index',
    component: ()=> import(/* webpackChunkName: "home" */'../pages/home.vue'),
    children:[
      {
        path: '/index',
        name: 'index',
        component: ()=> import(/* webpackChunkName: "index" */'../pages/index.vue')
      },{
        path: '/userCenter',
        name: 'userCenter',
        component: ()=> import(/* webpackChunkName: "usercenter" */'../pages/userCenter.vue')
      },{
        path: '/course',
        name: 'course',
        component: ()=> import(/* webpackChunkName: "course" */'../pages/course.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes,
  mode:"hash"
})

// 添加全局导航守卫
router.beforeEach((to,from,next) =>{
  if(to.name=='login'){
    return next()
  }
  
  if(!store.state.userInfo){
    return next({ name: 'login' })
  }else{
    return next()
  }
  
})

export default router
